package com.detection.hikvision.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.detection.common.domain.YqCameraInfo;
import com.detection.common.perimeter.hikvision.DeviceStatus;
import com.detection.hikvision.cache.CameraInfoCache;
import com.detection.hikvision.config.CusArtemisConfig;
import com.detection.hikvision.domain.CameraPreviewResultView;
import com.detection.hikvision.domain.RootRegionsResultView;
import com.detection.hikvision.service.ArtemisCamerasService;
import com.detection.hikvision.service.ArtemisRegionsService;
import com.detection.hikvision.utils.ArtemisApi;
import com.detection.hikvision.utils.HaiKangHttpUtils;
import org.assertj.core.util.Arrays;
import org.assertj.core.util.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author LH
 * @date 2022/10/19 下午2:39
 **/
@Service
public class ArtemisCamerasServiceImpl implements ArtemisCamerasService {

    private static final Logger logger = LoggerFactory.getLogger(ArtemisCamerasServiceImpl.class);

    @Resource
    private CusArtemisConfig cusArtemisConfig;


    @Autowired
    private ArtemisRegionsService artemisRegionsService;


    /**
     * 获取监控点树信息
     * @param pageNo 页码
     * @param pageSize 每页查询数量
     * @return
     */
    @Override
    public String getCameraTreeList( Integer pageNo, Integer pageSize) {

        //获取区域列表
        JSONArray resultArray = artemisRegionsService.getRegionsTree("camera",null);

        int size  = resultArray.size();
        String[] regionIndexCodes = new String[size];
        JSONObject jsonObject = new JSONObject();
        for(int i=0;i<size;i++){
            regionIndexCodes[i] = resultArray.getJSONObject(i).getString("indexCode");
            resultArray.getJSONObject(i).put("isRegion",1);
        }
        jsonObject.put("regionIndexCodes", regionIndexCodes);
        jsonObject.put("pageNo", pageNo);
        jsonObject.put("pageSize", pageSize);
        jsonObject.put("isSubRegion", true);
        jsonObject.put("authCodes", new String[]{"view"});
        HaiKangHttpUtils haiKangHttpUtils = new HaiKangHttpUtils();
        String send = haiKangHttpUtils.send(cusArtemisConfig, jsonObject, ArtemisApi.GET_CAMERA_LIST);
        JSONObject result = JSON.parseObject(send);
        JSONObject data =result.getJSONObject("data");

        JSONArray list = data.getJSONArray("list");

        int length = list.size();
        for (int i=0;i<length;i++){
            JSONObject oneObj = new JSONObject();
            oneObj.put("indexCode",list.getJSONObject(i).getString("indexCode"));
            oneObj.put("name",list.getJSONObject(i).getString("name"));
            oneObj.put("parentIndexCode",list.getJSONObject(i).getString("regionIndexCode"));
            oneObj.put("isRegion",0);
            resultArray.add(oneObj);
        }
        result = new JSONObject();
        result.put("code",0);
        result.put("msg","success");
        result.put("data",resultArray);
        String  ss =JSON.toJSONString(result);
        System.out.println(ss);
        return ss;
    }

    @Override
    public JSONArray getCameraOnlineList(Integer pageNo, Integer pageSize,List<String> indexCodesList) {
        JSONObject jsonObject = new JSONObject();
        String[] array = indexCodesList.stream().toArray(String[]::new);
        jsonObject.put("indexCodes", array);
        jsonObject.put("pageNo", pageNo);
        jsonObject.put("pageSize", pageSize);

        HaiKangHttpUtils haiKangHttpUtils = new HaiKangHttpUtils();
        String send = haiKangHttpUtils.send(cusArtemisConfig, jsonObject, ArtemisApi.GET_CAMERA_ONLINE_LIST);
        JSONObject result = JSON.parseObject(send);
        JSONObject data =result.getJSONObject("data");

        JSONArray list = data.getJSONArray("list");
        JSONArray resultArray = new JSONArray();
        int length = list.size();
        for (int i=0;i<length;i++){
            JSONObject oneObj = new JSONObject();
            oneObj.put("indexCode",list.getJSONObject(i).getString("indexCode"));
            oneObj.put("online",list.getJSONObject(i).getString("online"));
            resultArray.add(oneObj);
        }
        System.out.println(resultArray);
        return resultArray;
    }
    @Override
    public CameraPreviewResultView getCameraPreviewUrls(String cameraIndexCode, Integer streamType, String protocol, Integer transmode) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", null == streamType ? 1 : streamType);
        jsonBody.put("protocol", Objects.isNull(protocol) ? "rtsp" : protocol);
        jsonBody.put("transmode",null == transmode ? 1 : transmode);
        HaiKangHttpUtils haiKangHttpUtils = new HaiKangHttpUtils();
        String send = haiKangHttpUtils.send(cusArtemisConfig, jsonBody, ArtemisApi.GET_CAMERA_PREVIEW_URLS);
        return !ObjectUtils.isEmpty(send) ? JSON.parseObject(send, CameraPreviewResultView.class) : null;
    }

    @Override
    public String getDoorEvents(String startTime, String endTime, Integer pageNo, Integer pageSize) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        jsonBody.put("startTime", startTime);
        jsonBody.put("endTime", endTime);
        HaiKangHttpUtils haiKangHttpUtils = new HaiKangHttpUtils();
        String send = haiKangHttpUtils.send(cusArtemisConfig, jsonBody, ArtemisApi.GET_DOOR_EVENTS);

        return send;
    }

}
